<template>
    <div>
        <el-table :data="menuData" border style="width: 100%" row-key="id" :tree-props="{ children: 'children' }"
            default-expand-all>
            <el-table-column prop="id" label="菜单编号" width="150">
            </el-table-column>
            <el-table-column prop="title" label="菜单名称" width="120">
            </el-table-column>
            <el-table-column prop="pid" label="上级菜单" width="120">
            </el-table-column>
            <el-table-column label="菜单图标" width="120">
                <template #default="item">
                    <div>
                        <el-icon>
                        </el-icon>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="url" label="菜单地址" width="300">
            </el-table-column>
            <el-table-column label="状态" width="120">
                <template #default="item">
                    <div>
                        <el-tag v-if="item.row.status == 1" type="success">启用</el-tag>
                        <el-tag type="danger" v-else>禁用</el-tag>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template #default="item">
                    <div>
                        <el-button type="primary" circle :icon="Edit" @click="edit(item.row.id)">
                        </el-button>
                        <el-button type="danger" circle :icon="Delete" @click="del(item.row.id)">
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script setup lang="ts">
import {
    Delete,
    Edit
} from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus';
import type { Action } from 'element-plus'
import { defineEmits, computed } from "vue"
import { useStore } from "vuex"
//引入接口地址
import { menuList, menuDel } from "../../request/api"
let store = useStore()
//去定义 子父组件通信方法 创建自定义事件名称
let emit = defineEmits(["edit"])
let edit = (id: number) => {
    // 子传父
    emit("edit", id)
}
const del = (id: number) => {
    ElMessageBox.confirm(
        '你确定要删除吗?',
        '提示',
        {
            distinguishCancelAndClose: true,
            confirmButtonText: '确定',
            cancelButtonText: '取消',
        }
    )
        .then(() => {
            // 调用删除接口
            menuDel({ id }).then((res: any) => {
                if (res.data.code == 200) {
                    ElMessage({
                        type: 'success',
                        message: res.data.msg
                    })
                    store.dispatch("menu/menuList_Action")
                }
            })
        })
        .catch((action: Action) => {
            ElMessage({
                type: 'error',
                message: "取消删除"
            })
        })
}
const menuData = computed(() =>
    store.getters["menu/getMenuList"]
)
store.dispatch("menu/menuList_Action")
</script>

<style scoped>
</style>
